package CommonGui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;

public class TranslateDigits implements ActionListener {


	public String text;
	public byte CAPS=1;
	public String st;
	public char symbol;
	public JTextField TextField;
	public JFormattedTextField  formattedTextField;
	public boolean language=true;
	public boolean Reg=true;
	public int position;
	public int KBSwitch;                      // KBSwitch=0   ->  JTextfield
	                                          // KBSwitch=1   ->  FormattedTextField
	
	public TranslateDigits()
	{

	}
	
	public JButton key[];



	
public void init() {

			
			key[0]=new JButton2("1");
			key[1]=new JButton2("2");
			key[2]=new JButton2("3");
			key[3]=new JButton2("4");
			key[4]=new JButton2("5");
			key[5]=new JButton2("6");
			key[6]=new JButton2("7");
			key[7]=new JButton2("8");
			key[8]=new JButton2("9");
			key[9]=new JButton2(" ");
			key[10]=new JButton2("0");
			key[11]=new JButton2(".");
			
	
	}

	

public void listen() 
{
		key[0].addActionListener(this);	
		key[1].addActionListener(this);
		key[2].addActionListener(this);		
		key[3].addActionListener(this);	
		key[4].addActionListener(this);
		key[5].addActionListener(this);
		key[6].addActionListener(this);
		key[7].addActionListener(this);
		key[8].addActionListener(this);
		key[9].addActionListener(this);
		key[10].addActionListener(this);
		key[11].addActionListener(this);				
};


	/*public void Write(char symbols) {
		
		if(KBSwitch==0)
		{
		if(TextField!=null)
		{
		String txt=TextField.getText();
		String begin=txt.substring(0, position);
		String end=txt.substring(position, txt.length());
		String txt2=begin +symbols + end;
		this.position++;	
		TextField.setText(txt2);
		}
		}
		
		
		
		
		if (KBSwitch==1)
		{
			
		if(formattedTextField!=null)
		{
									
			if (position==4)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=txt.substring(0, 3)+symbols;
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position++;
			}
			
			
			

			if (position==3)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=txt.substring(0, 2)+symbols+txt.substring(3, 4);
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position++;
			}
			
			
			
			if (position==1)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=txt.substring(0, 1)+symbols+txt.substring(2, 4);
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position=position+2;
			}
			
			
			if (position==0)
			{
				String ftfText=formattedTextField.getText();
				String txt=ftfText.substring(0, 2) + ftfText.substring(3, 5);
				String txtForSet=symbols+txt.substring(1, 4);
				System.out.println(txtForSet + "        position :     "+position+ "                 ftfText : "+ ftfText);
				formattedTextField.setText(txtForSet);
				position++;
			}
			
			
				
		
		}
		}
		
	};*/
	
	
	 public void actionPerformed(ActionEvent e)
	    {
	        String actionCommand = e.getActionCommand();
	        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
	        JTextField txtF=(JTextField)component;
	        int caret=txtF.getCaretPosition();
	        String text=txtF.getText();
	        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
	        txtF.setCaretPosition(caret+1);
	        //txt.setText(txt.getText() + actionCommand);
	    }
	
	public class JButton2 extends JButton
	{
		public JButton2(String str)
		{
			super(str);
			this.setFocusable(false);
			this.setFocusPainted(false);
			this.setPreferredSize(new Dimension(100, 100));
	        Font font = this.getFont();
	        float size = font.getSize() + 15.0f;
	        this.setFont(font.deriveFont(size));
		}
	}
	

}